
package com.ikamobile.flickrer;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class ImageViewerActivity extends Activity {
    static final String EXTRA_IMAGE_URL = "IMAGE_URL";
    private ImageView mImageView;
    private ProgressBar mProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_viewer);
        mImageView = (ImageView) findViewById(R.id.photo);
        mProgress = (ProgressBar) findViewById(R.id.progress);
        populate();
    }

    private void populate() {
        mProgress.setVisibility(View.VISIBLE);
        String photoUrl = getImageUrl();
        new PhotoHelper(this).loadOrSetImage(mImageView, photoUrl, mProgress);
    }

    private String getImageUrl() {
        return getIntent().getStringExtra(EXTRA_IMAGE_URL);
    }

    public static void launch(Context context, String imageUrl) {
        Intent intent = new Intent(context, ImageViewerActivity.class);
        intent.putExtra(EXTRA_IMAGE_URL, imageUrl);
        context.startActivity(intent);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add("Share").setIcon(android.R.drawable.ic_menu_share)
                .setOnMenuItemClickListener(new OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        share();
                        return true;
                    }
                });
        return true;
    }

    protected void share() {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_TEXT, getImageUrl());
        intent.setType("text/plain");
        startActivity(Intent.createChooser(intent, "Share via"));
    }
}
